﻿<cfscript>
/** 
* @hint "acadmicBase - Classroom"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.classroomListener" 
	output=false 
	depends="classroomUsageAdvice, classroomTypeUsageAdvice, sequenceClassroomType, sequenceClassroom"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 


	/**
	* @hint "更新教室类别"
	* 
	* @event "MachII Event Object"
	*/
	public void function createClassroomType( required MachII.framework.Event event ) output=false {
		
		var usageAdvice = getclassroomTypeUsageAdvice();
		var sequenceClassroomType = getsequenceClassroomType();

		/* Every SQL operation is TRANSACATION*/
		transaction {
									
			var typeId = sequenceClassroomType.getID();
	
			/* 类别可见学院 */
			var specialDepartment = "";
			
			if ( event.getArg("FullInstitute") eq "1" ) {
				specialDepartment = "ALL";
			}
			else {
				specialDepartment = event.getArg('targetInstitute');
			}
			
			var sql = "	INSERT INTO 
							t_classroom_type
						(
							cr_type,
							cr_type_name,
							cr_usage,
							spec_deps
						)VALUES(
							:typeId ,
							:typeName ,
							:usage ,
							:specialDepartment 
						)";
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "typeName", value = event.getArg("TypeName"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "typeId", value = typeId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "specialDepartment", value = specialDepartment, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "usage", value = usageAdvice.stringUsage(), cfsqltype = "cf_sql_varchar");
			queryObj.execute( sql = sql );
		}
		
	}


	/**
	* @hint "更新教室类别"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateClassroomType( required MachII.framework.Event event ) output=false {
		
		var typeId = event.getArg("Type");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* 类别可见学院 */
			var specialDepartment = "";

			if ( event.getArg("FullInstitute") eq "1" ) {
				specialDepartment = "ALL";
			}
			else {
				specialDepartment = event.getArg('targetInstitute');
			}
			
			var sql = "	UPDATE
							t_classroom_type ct
						SET
							ct.cr_type_name = :typeName ,
							ct.spec_deps = :specialDepartment 
						WHERE
							ct.cr_type = :typeId ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "typeName", value = event.getArg("TypeName"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "typeId", value = typeId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "specialDepartment", value = specialDepartment, cfsqltype = "cf_sql_varchar");
			queryObj.execute( sql = sql );
		}
	}
	
	
	/**
	* @hint "删除教室类别"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteClassroomType( required MachII.framework.Event event ) output=false {
		
		var typeId = event.getArg("Type");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			var sql = "	DELETE FROM
							t_classroom_type ct 
						WHERE
							ct.cr_type = :typeId ";
							
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "typeId", value = typeId, cfsqltype = "cf_sql_char");
			queryObj.execute( sql = sql );
		}
		
	}
	
	

	/**
	* @hint "批量创建教学场地"
	* 
	* @event "MachII Event Object"
	*/
	public void function batchCreateClassroom( required MachII.framework.Event event ) output=false {
		var sequenceClassroom = getsequenceClassroom();
		var clsroomCount =  event.getArg("tarCount");
		var clsroomBuildName =  event.getArg("tarBuilding");
		var clsroomIndexLen =  event.getArg("tarIdx");
		
		var clsroomName = "";
		var clsroomFloor = "";
		var clsroomSitCount = 0;
		var clsroomSitForCourse = 0;
		var clsroomSitForExam = 0;
		
		var clsroom = "";
		var campus = "";
		var build = "";
		var typer = "";
		var purpose = "";
		
		var id = "";
		var i = 0;
		
		var usageAdvice = getclassroomUsageAdvice();
		
		/*
			教室用途属性设置
		*/
		usageAdvice.setRunning( yesNoFormat( event.getArg("Running", 0) ) );
		usageAdvice.setForCourse( yesNoFormat( event.getArg("ForCourse", 0) ) );
		usageAdvice.setForExam( yesNoFormat( event.getArg("ForExam", 0) ) );
		usageAdvice.setForBorrow( yesNoFormat( event.getArg("ForBorrow", 0) ) );
		usageAdvice.setForStudy( yesNoFormat( event.getArg("ForStudy", 0) ) );

		/* 
			楼层标识 01 表示 第一楼, B1 表示 地下1楼 地下标识可能用于地势复杂区域 存在入口在楼宇中央的情景
		*/
		clsroomFloor = event.getArg("Floor");
		if ( isNumeric(clsroomFloor) ) {
			clsroomFloor = numberFormat(clsroomFloor, '0_');
		}
		
		/*
			教室座位数设置
		*/
		if ( isNumeric(event.getArg("SitCount")) ) {
			clsroomSitCount = event.getArg("SitCount");
		}
		if ( usageAdvice.getForCourse() and isNumeric(event.getArg("SitForCourse")) ) {
			clsroomSitForCourse = event.getArg("SitForCourse");
		}
		if ( usageAdvice.getForExam() and isNumeric(event.getArg("SitForExam")) ) {
			clsroomSitForExam = event.getArg("SitForExam");
		}
		
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sql = "	SELECT
							b.campus_id
						FROM
							t_building b
						WHERE
							b.b_id = :bid ";
			
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam(name = "bid", value = event.getArg("BuildingID"), cfsqltype = "cf_sql_char");
			rs_buildingEntity = queryObj.execute( sql = sql ).getResult();
			
			
			/* 读取最大的教室编号 */
			sequenceClassroom.setBuildingID(event.getArg("BuildingID"));
			sequenceClassroom.setFloor(clsroomFloor);
			id = sequenceClassroom.getID();

			for ( i=1; i <=clsroomCount; i++ ) {

				/* 动态生成教室名称 */
				clsroomName = event.getArg("tarBuilding") & event.getArg("Floor") & right(id, 2);

				sql = "INSERT INTO 
							t_classroom
						(
							cr_id,
							campus_id,
							b_id,
							cr_type,
							classroom,
							cr_purpose,
							cr_floor,
							cr_usage,
							sits,
							sits_course,
							sits_exam
						)VALUES(
							:id ,
							:campusId ,
							:bid ,
							:typeId ,
							:name ,
							:purpose ,
							:floor ,
							:usage ,
							:sits ,
							:forCourse ,
							:forExam 
						)";
						
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "id", value = id, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "campusId", value = rs_buildingEntity.campus_id, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "bid", value = event.getArg("BuildingID"), cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "typeId", value = event.getArg("ClassroomType"), cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "name", value = clsroomName, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "purpose", value = event.getArg("ClassroomPurpose"), cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "floor", value = clsroomFloor, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "usage", value = usageAdvice.stringUsage(), cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "sits", value = clsroomSitCount, cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "forCourse", value = clsroomSitForCourse, cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "forExam", value = clsroomSitForExam, cfsqltype = "cf_sql_integer");
				queryObj.execute( sql = sql );
				
				/* 顺序创建教室编号 使用最后2位作为流水号 */
				id = left(id, (len(id)-2)) & numberFormat((right(id,2)+1), '0_');
			
			}
			
			event.setArg("CID", rs_buildingEntity.campus_id);
			event.setArg("BID", event.getArg("BuildingID"));
			event.setArg("TabID", "labClassroom");
			
		}
		
		
	}
	
}

</cfscript>
